package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.IDishFlavorService;
import com.sky.service.IDishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品相关接口
 *
 * @Author mr.Du
 * @Date 2025/5/21 2025/5/21
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {

    @Autowired
    private IDishService dishService;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private IDishFlavorService dishFlavorService;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    /**
     * 新增菜品
     *
     * @param dto
     * @return
     */
    @PostMapping
    public Result addDish(@RequestBody DishDTO dto) {
        dishService.insertDish(dto);
        return Result.success();
    }

    /**
     * 查询菜品列表
     *
     * @return
     */
    @GetMapping("/page")
    public Result page(DishPageQueryDTO dto) {
        PageResult pageResult = dishService.pageDish(dto);
        return Result.success(pageResult);
    }

    /**
     * 批量删除菜品
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestParam("ids") List ids) {
        dishMapper.deleteBatchIds(ids);
        List<DishFlavor> dishFlavors = dishFlavorService.lambdaQuery()
                .in(DishFlavor::getDishId, ids).list();
        for (DishFlavor dishFlavor : dishFlavors) {
            dishFlavorMapper.deleteById(dishFlavor.getId());
        }
        return Result.success();
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */

    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        DishVO dishVO = new DishVO();
        Dish dish = dishService.lambdaQuery().eq(Dish::getId, id).one();
        List<DishFlavor> list = dishFlavorService.lambdaQuery().eq(DishFlavor::getDishId, id).list();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(list);
        return Result.success(dishVO);
    }

    /**
     * 更新菜品
     *
     * @param dto
     * @return
     */
    @PutMapping
    public Result updateDish(@RequestBody DishDTO dto) {
        dishService.updateDish(dto);
        return Result.success();
    }

    /**
     * 修改菜品状态
     *
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable("status") Integer status, Integer id) {
        dishService.lambdaUpdate()
                .eq(Dish::getId, id)
                .set(Dish::getStatus, status)
                .update();
        return Result.success();
    }

    /**
     * 根据分类查询菜品
     *
     * @return
     */
    @GetMapping("/list")
    public Result listByCategory(@RequestParam("categoryId") Integer categoryId) {
        List<Dish> dishList = dishService.lambdaQuery().eq(Dish::getCategoryId, categoryId).list();
        return Result.success(dishList);
    }
}
